/**
 * Filename: GACopyManager.js
 * Author: Abid Hussain
 * Date Created: 13/07/2011
 */
exports.GACopyManager = Core.Class.singleton({
	classname: 'GACopyManager',
	initialize: function(){
		this._readCount = 0;
		this._writeCount = 0;
		this._deleteCount = 0;
		this._totalFiles = 0;
		this._readError = false;
		this._writeError = false;
		this._deleteError = false;
	},
	copy: function(fileNames, sourcePath ,destPath, deleteSourceFiles, progressCallback, doneCallback, errorCallback){
		this._fileNames= fileNames;
		this._sourcePath = sourcePath;
		this._destPath = destPath;
		this._deleteSourceFiles = deleteSourceFiles;
		this.progressCallback = progressCallback;
		this.doneCallback = doneCallback;
		this.errorCallback = errorCallback;
		this._totalFiles = this._fileNames.length;
		for(var count = 0; count < this._totalFiles; count++){
			var name = this._fileNames[count];
			this._readFile(name);
		}
	},
	_readFile: function(filename){
		var path = this._sourcePath + "/" + filename;
		Storage.FileSystem.readFile(path, false, this._readFileCallback.bind(this, filename));
	},
	_readFileCallback: function(filename,error,contents){
		this._readCount ++;
		if(!error){
			this._writeFile(filename,contents);
		}
		else{
			this._readError = true;
		}
		if(this._readCount == this._totalFiles){
			this._readCount = 0;
			if(this._readError){
				this._readError = false;
				this.errorCallback();
			}
		}
	},
	
	_writeFile: function(filename,contents){
		var path = this._destPath+"/"+filename; 
		Storage.FileSystem.writeFile(path, contents, false, this._writeFileCallback.bind(this))
	},
	
	_writeFileCallback: function(error){
		this._writeCount ++;
		if(error){
			this._writeError = true;
		}
		else{
			this.progressCallback(this._writeCount, this._totalFiles);
		}
		if( this._writeCount == this._totalFiles){
			this._writeCount = 0;
			if(this._writeError){
				this._writeError = false;
				this.errorCallback();
			}
			else{
				if(this._deleteSourceFiles){
					this._deleteFiles();
				}
				else{
					this.doneCallback();
				}
			}
		}
	},
	
	_deleteFiles: function(){
		for(var i=0; i<this._totalFiles; i++){
			var path = this._sourcePath + "/" + this._fileNames[i];
			Storage.FileSystem.deleteFile(path,this._deleteFileCallback.bind(this));
		}
	},
	
	_deleteFileCallback: function(error,data){
		this._deleteCount ++;
		if(error){
			this._deleteError = true;
		}
		if( this._deleteCount == this._totalFiles ){
			this._deleteCount = 0;
			if(this._deleteError){
				this._deleteError = false;
				this.errorCallback();
			}
			else{
				this.doneCallback();
			}
		}
	},
	
	destroy: function(){
		this._readCount = null;
		this._writeCount = null;
		this._totalFiles = null;
		this._readError = null;
		this._writeError = null;
		this._fileNames= null;
		this._sourcePath = null;
		this._destPath = null;
		this.progressCallback = null;
		this.doneCallback = null;
		this.errorCallback = null;
		this._deleteCount = null;
		this._deleteError = null;
		this._deleteSourceFiles = null;
	}
	
});